<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <div class="img">
          <img src="../assets/logo.png">
        </div>
      </div>
      <el-form :model="loginForm" ref="loginForm" :rules="loginFormRules" class="login_form">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" prefix-icon="el-icon-lock" type="password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
          <el-button type="info" @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginFormRules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate (async valid => {
        if (valid) {
          window.sessionStorage.setItem("token","admin");
          window.localStorage.setItem("token","admin");
          this.$router.push('/home');
          this.$notify({
            title: '登录提醒',
            message: '登录成功!',
            duration: 2000
          });
          // this.$msg.success('登录成功！');
          const {data: result} = await this.$http.post('/test', this.loginForm);
          console.log(result);
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm (formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
.login_container{
  height: 100%;
}
.login_box{
  width: 450px;
  height: 300px;
  background-color: #F0F8FF;
  border-radius: 20px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
}
.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #F0F8FF;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #F0F8FF;
  position: absolute;
  left: 50%;
  transform: translate(-50%,-50%);
  background-color: #E4E7ED;
}
.avatar_box img {
  width: 130px;
  height: 130px;
  border-radius: 50%;
}

.login_form{
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns{
  display: flex;
  justify-content: flex-end;
}
</style>
